package lsem.eclipse.vdf.ui.handler;


import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.EditorPane;
import org.eclipse.ui.internal.PartPane;
import org.eclipse.ui.internal.WorkbenchPartReference;
import org.eclipse.ui.IPartListener2;
import org.neo4j.neoclipse.view.NeoGraphViewPart;

//Create a Class to implement IpartListener2

@SuppressWarnings("restriction")
public class EditorPaneListener implements IPartListener2 {

	// The Page upon which we have to implement the minimize event

	private IWorkbenchPage currentPage;

	public EditorPaneListener(IWorkbenchPage currentPage) {
		this.currentPage = currentPage;
	}

	public void partActivated(IWorkbenchPartReference partRef) {
	}

	public void partBroughtToTop(IWorkbenchPartReference partRef) {
	}

	public void partClosed(IWorkbenchPartReference partRef) {
	}

	public void partDeactivated(IWorkbenchPartReference partRef) {
	}

	public void partHidden(IWorkbenchPartReference partRef) {

		IEditorReference[] editor = currentPage.getEditorReferences();

		// Make sure that this is handled if the EditorPane has no more pages
		// opened on it
		if (editor.length < 1) {
			PartPane pane = ((WorkbenchPartReference) partRef).getPane();
			if (pane instanceof EditorPane) {

				// Set the EditotPane part yo minimized state
				currentPage.setPartState(partRef,
						IWorkbenchPage.STATE_MINIMIZED);

				try {
					currentPage.showView(NeoGraphViewPart.ID, null,
							IWorkbenchPage.VIEW_VISIBLE);
				} catch (PartInitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	public void partInputChanged(IWorkbenchPartReference partRef) {
	}

	public void partOpened(IWorkbenchPartReference partRef) {
	}

	public void partVisible(IWorkbenchPartReference partRef) {
	}

}